import { BasicColumn, FormSchema } from '@/components/Table';
import { doAdminStatusUpdate, getDeptList, getRoleList } from '@/modules/system/api';
import { h } from 'vue';
import { Switch } from 'ant-design-vue';

export const ColumnPage: BasicColumn[] = [
  {
    title: '用户账号',
    dataIndex: 'username',
  },
  {
    title: '姓名',
    dataIndex: 'name',
    width: 100,
  },
  {
    title: '角色',
    dataIndex: 'roleName',
  },
  {
    title: '所属部门',
    dataIndex: 'deptName',
  },
  {
    title: '手机号',
    dataIndex: 'phone',
  },
  {
    title: '备注',
    dataIndex: 'remark',
  },
  {
    title: '状态',
    dataIndex: 'status',
    fixed: 'right',
    width: 110,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === 1,
        checkedChildren: '正常',
        unCheckedChildren: '禁用',
        loading: record.pendingStatus,
        onChange(checked: boolean) {
          record.pendingStatus = true;
          const newStatus = checked ? 1 : 0;
          doAdminStatusUpdate({ adminId: record.adminId, status: newStatus })
            .then(() => {
              record.status = newStatus;
            })
            .catch(() => {})
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '添加时间',
    dataIndex: 'createTime',
    width: 130,
    fixed: 'right',
    customRender: ({ record }) => {
      return record.createTime?.substring(0, 11);
    },
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'username',
    label: '用户账号',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'nickname',
    label: '姓名',
    component: 'Input',
    colProps: { span: 8 },
  },
];

export const FormSchemaPassword: FormSchema[] = [
  {
    field: 'password',
    label: '新密码',
    component: 'Input',
    required: true,
  },
];
export const FormSchemaAdd: FormSchema[] = [
  {
    field: 'username',
    label: '登录账号',
    component: 'Input',
    helpMessage: ['登录账号,如姓名的全拼字母'],
    required: true,
    componentProps: {
      placeholder: '请输入登录的用户账号',
    },
  },
  {
    field: 'password',
    label: '密码',
    component: 'InputPassword',
    required: true,
  },
  {
    label: '角色',
    field: 'roleId',
    component: 'ApiSelect',
    componentProps: {
      api: getRoleList,
      labelField: 'roleName',
      valueField: 'id',
    },
    required: true,
  },
  {
    field: 'deptId',
    label: '所属部门',
    component: 'ApiTreeSelect',
    componentProps: {
      api: getDeptList,
      // params: { type: 'select' },
      resultField: 'list',
      labelField: 'deptName',
      valueField: 'id',
    },
    required: true,
  },
  {
    field: 'name',
    label: '真实姓名',
    component: 'Input',
    required: true,
  },

  {
    label: '手机号',
    field: 'phone',
    component: 'Input',
  },

  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
  },
];
